<?php
namespace App\Http\Controllers\Admin;

use App\Models\Achievement;
use App\Models\Group;
use App\Models\User;
use App\Models\Attendence;
use App\Models\Performance;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Input;
use DB;


class PerformanceController extends BaseController
{


    public function __construct() {
        parent::__construct();
    }


    public function getIndex(Request $request)
    {
        $users = User::where('role_id','=','0')->orderBy('created_at')->get();

        return view('admin.kaohe.performance.index')
            ->with('users', $users);
    }



    public function getDetail(Request $request)
    {
        $uid = $request->input('uid','0');
        if($uid <=0){
            abort(404);
        }
        $dateid = $request->input('dateid','0');
        $date = Achievement::where('user_id',$uid)->lists('date');
        if($dateid <= 0){
            $att = Achievement::where('user_id',$uid)->orderBy('date','desc')->first();
        }else{
            $att = Achievement::where('user_id',$uid)->where('date',$dateid)->first();
        }
        return view("admin.kaohe.performance.detail")
            ->with('date',$date)
            ->with('uid',$uid)
            ->with('att',$att);
    }


    //添加考勤
    public function getPerfAdd(Request $request)
    {
        $uid = $request->input('uid','0');
        if($uid <=0){
            abort(404);
        }
        $dateid = $request->input('dateid','0');
        if($dateid <=0){
            $dateid = date('Ym',time());
        }
        return view("admin.kaohe.performance.add")
            ->with('uid',$uid)
            ->with('dateid',$dateid)
            ->with('blademode','add');
    }


    public function getPerfEdit(Request $request)
    {
        $uid = $request->input('uid','0');
        if($uid <=0){
            abort(404);
        }
        $dateid = $request->input('dateid','0');
        if($dateid <=0){
            abort(404);
        }
        $att = Achievement::where('user_id',$uid)->where('date',$dateid)->first();
        return view("admin.kaohe.performance.add")
            ->with('att',$att)
            ->with('uid',$uid)
            ->with('dateid',$dateid)
            ->with('blademode','edit');
    }


    public function postPerfStore(Request $request)
    {
        $blade = $request->input('blademode','add');
        $user_id = $request->input('uid','0');
        $data = $request->only('dateid','score','goal','achievement');
        $id = $request->input('id','0');
        $year = substr($data['dateid'],0,-2);
        $percent = $request->input('percent','0');
        if($blade == 'edit')
        {
            $att = Achievement::find($id);
            if(empty($att)){
                abort(404);
            }
            DB::beginTransaction();
            try{
                $att->goal = $data['goal'];
                $att->achievement = $data['achievement'];
                $att->score = $data['score'];
                $att->year = $year;
                $att->percent = $percent;
                $att->save();
                //计算累计业绩得分,写入绩效表
                $uach = Achievement::where('user_id',$user_id)->where('year',$year)->get();
                $tscore = 0;// 业绩总分
                $ascore = 0;//用户总得分
                foreach ($uach as $u)
                {
                    $ascore += $u->score * $u->percent;
                    $tscore += $u->score * 100;
                }
                $perform = Performance::where('user_id',$user_id)->where('date',$year)->first();
                if(empty($perform))
                {
                    $perform = new Performance();
                    $perform->user_id = $user_id;
                    $perform->date = $year;
                }
                $perform->achievement_score = $ascore;
                $perform->total_ach_score = $tscore;
                //$perform->totalscore += $tscore;
                $perform->save();
                DB::commit();
                return Response::json(array('id'=>$att->id,'status'=>'success','msg'=>'用户数据保存成功！','redirectUrl'=>'/admin/performance/index'));
            }catch (PDOException $e) {
                Log::error(['存储异常' => $e->getMessage()]);
                DB::rollBack();
                return Response::json(array('id'=>$att->id,'status'=>'fail','msg'=>'用户数据保存失败！'));
            }
        }
        else
        {
            DB::beginTransaction();
            try {
                //保存该月业绩
                $att = new Achievement();
                $att->user_id = $user_id;
                $att->goal = $data['goal'];
                $att->achievement = $data['achievement'];
                $att->score = $data['score'];
                $att->date = $data['dateid'];
                $att->year = $year;
                $att->percent = $percent;
                $att->save();
                 //计算累计业绩得分,写入绩效表
                $uach = Achievement::where('user_id',$user_id)->where('year',$year)->get();
                $tscore = 0;// 业绩总分
                $ascore = 0;//用户总得分
                foreach ($uach as $u)
                {
                    $ascore += $u->score * $u->percent;
                    $tscore += $u->score * 100;
                }
                $perform = Performance::where('user_id',$user_id)->where('date',$year)->first();
                if(empty($perform))
                {
                    $perform = new Performance();
                    $perform->user_id = $user_id;
                    $perform->date = $year;
                }
                $perform->achievement_score = $ascore;
                $perform->total_ach_score = $tscore;
                //$perform->totalscore += $tscore;
                $perform->save();
                DB::commit();
                return Response::json(array('id'=>$att->id,'status'=>'success','msg'=>'用户数据保存成功！','redirectUrl'=>'/admin/performance/index'));
            } catch (PDOException $e) {
                Log::error(['存储异常' => $e->getMessage()]);
                DB::rollBack();
                return Response::json(array('id'=>$att->id,'status'=>'fail','msg'=>'用户数据保存失败！'));
            }
        }
    }



    public function postPerfDelete($id)
    {
        if(!$this->user->can('deleteCompanyJobsType'))
            return $this->formatJson('error',$this->permerror);

        $folder = Group::find($id);

        if ($folder->delete())
            return  Response::json(array('status'=>'success','msg'=>'职位分类删除成功!'));
        else
            return  Response::json(array('status'=>'error','msg'=>'删除失败'));
    }

}


